#pragma once
#include <QObject>
#include <QTimer>
#include <QDebug>
#include <QSharedPointer>
#include <QtSerialPort/QSerialPort>
#include <QtSerialPort/QSerialPortInfo>


struct SerialInfo
{
	QString comPort;
	unsigned int baudRate;
	unsigned int dataBits;
	unsigned int parity;
	unsigned int stopBits;
	unsigned int flowControl;
};

enum DeviceConnState
{
	UnconnectedState,
	ConnectingState,
	ConnectedState,
	ClosingState,
	ConnectTimeOut
};

enum DEVICE_CONN_TYPE
{
	ASYNC_AND_TIME_OUT, //Qt::QueuedConnection //timeout return
	ASYNC,  //Qt::QueuedConnection
	SYNC_FAQ_WAIT  //Qt::BlockingQueuedConnection
};

class ComSerialPport:public QObject
{
	Q_OBJECT
public:
	ComSerialPport(QSharedPointer<SerialInfo> serialInfo, DEVICE_CONN_TYPE comtype, QObject* parent = nullptr);
	~ComSerialPport();


signals:
	void sig_connection_state(int state);
	void sig_receiveDataFromServer(QString);

public slots:
	void slot_started();
	void slot_recv_device_data();
	void slot_readPortDataTimer();
	void slot_send_message(QString message);
	void slot_async_timeout_send_message(QString message, int write_timeout, int read_timeout);
	void slot_Reconnect(const QString& comport, int baudRate);



private:
	QSerialPort* m_port = nullptr;
	QSharedPointer<SerialInfo> m_serialInfo;
	DEVICE_CONN_TYPE  m_ComType;

	bool m_working = false;
	QString m_com_data;
	int m_timer_out = 0;
	QTimer* m_ASYNC_timer = nullptr;

	DeviceConnState m_connState = DeviceConnState::UnconnectedState;

};

